﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;
using BidFunLib.DAO;

namespace BidFunLib
{
    /// <summary>
    /// 
    /// </summary>
    class StatsBidUserGlobalDAO 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidUserGlobalDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StatsBidUserGlobal getStatsBidUserGlobalById(Int32 id)
        {
            StatsBidUserGlobal statsBidUserGlobal = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<StatsBidUserGlobal> statsBidUserGlobalList = (IList<StatsBidUserGlobal>)session.CreateQuery("from StatsBidUserGlobal bs where bs.IdStatsBidUserGlobal= ?")
                .SetInt32(0, id)
                .List<StatsBidUserGlobal>();

                if (statsBidUserGlobalList != null && statsBidUserGlobalList.Count > 0)
                {
                    statsBidUserGlobal = statsBidUserGlobalList.First();
                }
                transaction.Commit();
                session.Close();
                return statsBidUserGlobal;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="idBidUser"></param>
        /// <returns></returns>
        public StatsBidUserGlobal getStatsBidUserGlobalByIdBidUser(Int32 idBidUser)
        {
            StatsBidUserGlobal statsBidUserGlobal = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<StatsBidUserGlobal> statsBidUserGlobalList = (IList<StatsBidUserGlobal>)session.CreateQuery("from StatsBidUserGlobal bs where bs.IdBidUser= ?")
                .SetInt32(0, idBidUser)
                .List<StatsBidUserGlobal>();

                if (statsBidUserGlobalList != null && statsBidUserGlobalList.Count > 0)
                {
                    statsBidUserGlobal = statsBidUserGlobalList.First();
                }
                transaction.Commit();
                session.Close();
                return statsBidUserGlobal;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public StatsBidUserGlobal saveOrUpdate(StatsBidUserGlobal statsBidUserGlobal)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                if (statsBidUserGlobal.IdStatsBidUserGlobal == -1)
                {
                    log.Debug("Saving StatsBidUserGlobal...");
                    Int32 newID = (Int32)session.Save(statsBidUserGlobal);
                    statsBidUserGlobal.IdStatsBidUserGlobal = newID;
                    log.Debug("StatsBidUserGlobal saved !");
                }
                else
                {
                    log.Debug("Updating StatsBidUserGlobal...");
                    session.Update(statsBidUserGlobal);
                    log.Debug("StatsBidUserGlobal updated !");
                }
                transaction.Commit();
                session.Close();
                return statsBidUserGlobal;
            }
        }


    }
}
